package com.grj.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.grj.blog.a.ArticleService;
import com.grj.blog.config.Result;
import com.grj.blog.domain.BlogArticle;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/articles")
@Api(tags = "文章管理接口", description = "提供文章的增删改查及搜索功能")
public class ArticleController {
    private final ArticleService articleService;

    /**
     * 获取文章详情
     * @param id 文章ID
     * @return 文章详情
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "获取文章详情", notes = "根据文章ID获取文章详细信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "获取成功"),
            @ApiResponse(code = 404, message = "文章不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public Result<BlogArticle> detail(
            @ApiParam(name = "id", value = "文章ID", required = true, example = "1")
            @PathVariable @Min(1) Long id) {
        return Result.success(articleService.getArticleById(id));
    }

    /**
     * 发布文章
     * @param article 文章信息
     * @return 创建结果
     */
    @PostMapping("/publish")
    @ApiOperation(value = "发布文章", notes = "创建并发布新的文章")
    @ApiResponses({
            @ApiResponse(code = 200, message = "发布成功"),
            @ApiResponse(code = 400, message = "参数校验失败"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public Result<Void> publish(
            @ApiParam(name = "article", value = "文章信息", required = true)
            @RequestBody @Valid BlogArticle article) {
        articleService.publishArticle(article);
        return Result.success();
    }

    /**
     * 删除文章
     * @param id 文章ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除文章", notes = "根据文章ID删除指定文章")
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 404, message = "文章不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public Result<Void> delete(
            @ApiParam(name = "id", value = "文章ID", required = true, example = "1")
            @PathVariable @Min(1) Long id) {
        articleService.deleteArticle(id);
        return Result.success();
    }

    /**
     * 修改文章
     * @param article 文章信息
     * @return 修改结果
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "修改文章", notes = "更新指定文章的信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "修改成功"),
            @ApiResponse(code = 400, message = "参数校验失败"),
            @ApiResponse(code = 404, message = "文章不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public Result<Void> update(
            @ApiParam(name = "article", value = "文章信息", required = true)
            @RequestBody @Valid BlogArticle article) {
        articleService.updateArticle(article);
        return Result.success();
    }

    /**
     * 获取文章列表
     * @return 文章列表
     */
    @GetMapping
    @ApiOperation(value = "获取文章列表", notes = "获取所有文章列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "获取成功"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public Result<List<BlogArticle>> list() {
        List<BlogArticle> list = articleService.getArticleList();
        return Result.success(list);
    }

    /**
     * 搜索文章
     * @param keyword 关键字
     * @param pageNum 页码
     * @param pageSize 页大小
     * @return 文章列表
     */
    @GetMapping("/search")
    @ApiOperation(value = "搜索文章", notes = "根据关键字搜索文章，支持分页")
    @ApiResponses({
            @ApiResponse(code = 200, message = "搜索成功"),
            @ApiResponse(code = 400, message = "参数校验失败"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public Result<Page<BlogArticle>> search(
            @ApiParam(name = "keyword", value = "搜索关键字", required = true, example = "Java")
            @RequestParam @NotBlank String keyword,
            @ApiParam(name = "pageNum", value = "页码", defaultValue = "1", example = "1")
            @RequestParam(defaultValue = "1") @Min(1) int pageNum,
            @ApiParam(name = "pageSize", value = "页大小", defaultValue = "10", example = "10")
            @RequestParam(defaultValue = "10") @Min(1) int pageSize) {
        Page<BlogArticle> page = articleService.search(keyword, pageNum, pageSize);
        return Result.success(page);
    }
}
